package com.hu.network.http.request.impl;

import com.alibaba.fastjson.TypeReference;

import java.io.IOException;

import com.hu.network.http.bean.BaseRequestBean;
import com.hu.network.http.bean.InnerRequestBean;
import com.hu.network.http.other.MimeType;
import com.hu.network.http.request.BasePostHttpRequest;
import com.hu.network.util.JsonHelper;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import timber.log.Timber;

/**
 * 说明：
 *
 * @author ： hucanhua
 * @date ： 2017/10/25
 */
public class JsonPostHttpRequestImpl<T> extends BasePostHttpRequest<T> {

    private final InnerRequestBean<BaseRequestBean> innerRequestBean;
    private final TypeReference<T> tTypeReference;

    public JsonPostHttpRequestImpl(InnerRequestBean<BaseRequestBean> innerRequestBean, TypeReference<T> tTypeReference, final ProgressCallback requestCallback, final ProgressCallback responseCallback) {
        super(requestCallback, responseCallback);
        this.innerRequestBean = innerRequestBean;
        this.tTypeReference = tTypeReference;
    }

    @Override
    protected RequestBody getParams() {
        return getRequestBody(innerRequestBean.getParams(), MimeType.JSON);
    }

    @Override
    protected String getUrl() {
        return innerRequestBean.getUrl();
    }

    @Override
    protected T dealResponse(ResponseBody body) throws IOException {
        String bodyStr = body.string();
        Timber.d("数据:%s",bodyStr);
        return JsonHelper.parseObject(bodyStr, tTypeReference);
    }

    @Override
    public boolean useHttps() {
        return innerRequestBean.isHttps();
    }
}
